package com.snopy.thread.semaphore;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

/**
 * @author snopy
 * @version 1.0.0
 * @classname DemoSemaphore
 * @description
 * @email 77912204@qq.com
 * @date 2022/11/22 22:53
 */
public class DemoSemaphore {
    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(3);
        for (int i = 0;i < 10;i++){
            final int num = i;
            new Thread(() -> {
                try {
                    semaphore.acquire();
                    System.out.println("task..." + num);
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                semaphore.release();
            }).start();
        }
    }
}
